/*
*  Copyright 2019-2025 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.controller.inventory;


import lombok.RequiredArgsConstructor;
import java.util.List;

import me.zhengjie.annotation.Log;
import me.zhengjie.dao.InventoryTaskRecordDao;
import me.zhengjie.dao.dto.InventoryTaskRecordQueryCriteria;
import me.zhengjie.server.InventoryTaskRecordService;
import me.zhengjie.utils.PageResult;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;


/**
* @author laoxu
* @date 2025-07-04
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "InventoryRecordController")
@RequestMapping("/api/inventoryTaskRecord")
public class InventoryTaskRecordController {

    private final InventoryTaskRecordService inventoryTaskRecordService;

    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('inventoryTaskRecord:list')")
    public void exportInventoryTaskRecord(HttpServletResponse response, InventoryTaskRecordQueryCriteria criteria) throws IOException {
        inventoryTaskRecordService.download(inventoryTaskRecordService.queryAll(criteria), response);
    }

    @PostMapping("queryInventoryTaskRecord")
//    @ApiOperation("查询InventoryRecordController")
//    @PreAuthorize("@el.check('inventoryTaskRecord:list')")
    public ResponseEntity<PageResult<InventoryTaskRecordDao>> queryInventoryTaskRecord(@RequestBody  InventoryTaskRecordQueryCriteria criteria){
        if (criteria.getStartDate() != null && criteria.getStartDate().size() == 2 ){
            criteria.setStartTime(criteria.getCreateDate().get(0));
            criteria.setEndStartTime(criteria.getCreateDate().get(1));
        }
        if (criteria.getCreateDate() != null && criteria.getCreateDate().size() == 2 ){
            criteria.setCreateTime(criteria.getCreateDate().get(0));
            criteria.setEndCreateTime(criteria.getCreateDate().get(1));
        }

        Page<Object> page = new Page<>(criteria.getPage(), criteria.getSize());
        return new ResponseEntity<>(inventoryTaskRecordService.queryAll(criteria,page),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增InventoryRecordController")
    @ApiOperation("新增InventoryRecordController")
    @PreAuthorize("@el.check('inventoryTaskRecord:add')")
    public ResponseEntity<Object> createInventoryTaskRecord(@Validated @RequestBody InventoryTaskRecordDao resources){
        inventoryTaskRecordService.create(resources);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改InventoryRecordController")
    @ApiOperation("修改InventoryRecordController")
    @PreAuthorize("@el.check('inventoryTaskRecord:edit')")
    public ResponseEntity<Object> updateInventoryTaskRecord(@Validated @RequestBody InventoryTaskRecordDao resources){
        inventoryTaskRecordService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除InventoryRecordController")
    @ApiOperation("删除InventoryRecordController")
    @PreAuthorize("@el.check('inventoryTaskRecord:del')")
    public ResponseEntity<Object> deleteInventoryTaskRecord(@ApiParam(value = "传ID数组[]") @RequestBody List<String> ids) {
        inventoryTaskRecordService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}